package com.gopay.bis.service.bank.impl.bcdl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.manager.bank.NuccSerialManager;
import com.gopay.bis.manager.bank.bcdl.NuccBcdlManager;
import com.gopay.common.constants.bank.IcbcConstants;
import com.gopay.common.constants.bank.NuccConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BcdlMessage;
import com.gopay.common.domain.bank.BcdlResult;
import com.gopay.common.domain.bank.bcdl.BcdlPacket;
import com.gopay.common.domain.bank.bcdl.BcdlPacketRes;
import com.gopay.common.domain.bank.nucc.bcdl.eft.res.NuccBcdlRes;
import com.gopay.common.domain.bank.nucc.common.query.res.NuccQueryRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.BcdlInteractRemoteService;

/**
 * 网联付款接口实现类
 * 
 * @NuccBcdlServiceImpl.java
 * @author zhengxue
 * @2018-1-16 下午1:24:21 www.gopay.com.cn Inc.All rights reserved.
 */
public class NuccBcdlServiceImpl extends BaseBcdlServiceImpl implements BcdlInteractRemoteService {

    @Resource(name = "nuccBcdlManager")
    private NuccBcdlManager nuccBcdlManager;

    @Resource(name = "nuccSerialManager")
    private NuccSerialManager nuccSerialManager;

    @Override
    public BcdlResult eft(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        try {
            NuccBcdlRes data = (NuccBcdlRes) nuccBcdlManager.eft(base);
            res.setDefaultModel(data);
            if (data != null
                    && NuccConstants.NUCC_SYSRTNCD_SUCCESS.equals(data.getMsgBody().getSysRtnInf().getSysRtnCd())
                    && (NuccConstants.NUCC_TRXSTATUSCD_SUCCESS.equals(data.getMsgBody().getBizInf().getTrxStatus()) || NuccConstants.NUCC_EXCEPTION_SUCCESS
                            .equals(data.getMsgBody().getBizInf().getTrxStatus()))) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);
                res.setSuccess(true);
            } else if (data != null
                    && NuccConstants.NUCC_SYSRTNCD_SUCCESS.equals(data.getMsgBody().getSysRtnInf().getSysRtnCd())
                    && (NuccConstants.NUCC_TRXSTATUSCD_FAIL.equals(data.getMsgBody().getBizInf().getTrxStatus()) || NuccConstants.NUCC_EXCEPTION_FAIL
                            .equals(data.getMsgBody().getBizInf().getTrxStatus()))) {
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6027);
                res.setBankRetCode(data.getMsgBody().getBizInf().getBizStsCd());
                res.setErrorMsg(data.getMsgBody().getBizInf().getBizStsDesc());
                res.setSuccess(false);
                // modified by zyt,2017-10-16，设置是否需要做拒绝账务
                // res.setNeedRefund(isNeedRefund(data , base.getOrderId()));
            } else {
                // 如果流水号异常，调用流水号任务
                if (data != null
                        && data.getMsgBody() != null
                        && data.getMsgBody().getSysRtnInf() != null
                        && data.getMsgBody().getSysRtnInf().getSysRtnCd() != null
                        && (NuccConstants.NUCC_SYSRTNCD_SERIAL_ERROR.equals(data.getMsgBody().getSysRtnInf()
                                .getSysRtnCd()))) {
                    nuccSerialManager.getFlowIdRule();
                }
                if (data != null) {
                    if (data.getMsgBody() != null && data.getMsgBody().getSysRtnInf() != null) {
                        if (!NuccConstants.NUCC_SYSRTNCD_SUCCESS.equals(data.getMsgBody().getSysRtnInf().getSysRtnCd())) {
                            res.setBankRetCode(data.getMsgBody().getSysRtnInf().getSysRtnCd());
                            res.setErrorMsg(data.getMsgBody().getSysRtnInf().getSysRtnDesc());
                        } else if (data.getMsgBody().getBizInf() != null) {
                            res.setBankRetCode(data.getMsgBody().getBizInf().getBizStsCd());
                            res.setErrorMsg(data.getMsgBody().getBizInf().getBizStsDesc());
                        }
                    }
                }
                res.setResultCode(ProcCodeConstants.PROC_CODE_100E6066);
                res.setSuccess(false);
            }
            return res;
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult query(BcdlMessage base) {
        BcdlResult res = new BcdlResult();
        try {
            NuccQueryRes data = (NuccQueryRes) nuccBcdlManager.query(base);
            res.setDefaultModel(data);
            res.setSuccess(true);
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(e.getErrCode());
            res.setSuccess(false);
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
            res.setSuccess(false);
        }
        return res;
    }

    @Override
    public BcdlResult sendBank(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult sendBankPacket(BcdlPacket bcdlPacket) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlPacketRes queryPacket(BcdlPacket bcdlPacket) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult queryBalance(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult sendBankQueryBalance(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult eftRetry(BcdlMessage base) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public BcdlResult queryAcctDetail(BcdlMessage base) {
    	 BcdlResult res = new BcdlResult();
    	 Map map = new HashMap<String, Object>();
         try {
        	 map = (Map) nuccBcdlManager.qryAcctDetail(base);
        	 res.setSuccess(true);
             res.setDefaultModel(map);
             return res;
         } catch (GopayException e) {
             BankLogUtils.printServiceImplExceptionLog(e);
             res.setResultCode(e.getErrCode());
             res.setSuccess(false);
         } catch (Exception e) {
             BankLogUtils.printServiceImplExceptionLog(e);
             res.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
             res.setSuccess(false);
         }
         return res;
    }

}
